<?php
/**
 * The controller to manage information of medicine.
 *
 */
class C_ChronicDisease extends Controller {
	
	public $template_mod;
	
	function C_ChronicDisease($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'ChronicDesease') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'admin';
		}
		$this->template_mod = $template_mod;				
		$this->view->path = 'chronic_disease';
		$this->setLayout('admin');
	}
	/**
	 * Action to open the view for managing chronic disease.
	 */
	
	public function actionManagement() {
        return $this->view->render('chronic_disease_management.html');
	}
	
	public function actionListChronicDisease() {
		$chroList = Celini::newORDO('ChronicDisease')->listAll();
		$this->assign('chroList', $chroList);
		return $this->view->render('list.html');
	}
	// delete chronic disease
	public function actionChangeChronicDiseaseStatusAjax() {
    	$chronic_disease_id = $_GET['chronic_disease_id'];
    	$chronic = Celini::newORDO('ChronicDisease', $chronic_disease_id);
    	$status = $chronic->get('deleted');
        
        if ($status == 0) {
            $newStatus = 1;
        } else if ($status == 1) {
            $newStatus = 0;
        }
        $chronic->set('deleted', $newStatus);
        $chronic->persist();
    }
    
 /* Create a new chronic disease or modify a specified chronic disease. 
	 *
	 */
	public function actionEditChronicDisease() {
		$chronicId = $_GET['service_id'];
        $chronic = Celini::newORDO('ChronicDisease');
        if (isset($chronicId) && $chronicId != "") {
            $chronic->setup($chronicId);
        }
        $this->assign_by_ref('chronic', $chronic);
        return $this->view->render('edit_chronic_disease.html');
	}
	
	// save chronic disease
	public function actionSaveChronicDiseaseAjax() {
	   $chronic = Celini::newORDO('ChronicDisease');
	   if (isset($_POST['chronic_disease_id']) && $_POST['chronic_disease_id'] != "") {
            $chronic->setup($_POST['chronic_disease_id']);
       }
       $id	=	(int)$chronic->get_id();
       $name		=	Utils::sanitizeText($_POST['name']);
       $l10nData	=	array();
       if($id > 0){
			$field		=	L10nHelper::getLocale();
			$field 		=	L10nHelper::changeField($field, "name");
			$l10nData[$field]	=	$name;
       }
       // Disease has no name, add new localized name
       else{
			$supported	=	L10nHelper::getSupportedLocales();
			foreach ($supported as $locale	=>	$country){
				$locale 		=	L10nHelper::changeField($locale, "name");
				$l10nData[$locale]	=	$name;
			}
       }
       $chronic->populateArray($l10nData);
       $chronic->persist();
       return $chronic->get_id();
	}
}
?>